/**
 * This file is used for Padding by Ajax
 */
function paggingAjaxBuildTable(data)
{
	table = '<table id="'+data.tableName+'-table" width="980px;" class="display editInPlace" id="expensetable" border="0" cellpadding="0" cellspacing="1">';
	table +=  '<thead>';
	table +=	'<tr class="tablesorter-headerRow">';
	table += '<th class="checkbox"><input id="selectall_checkbox" class="selectall_checkbox" type="checkbox"></th>';
	
	$.each(data.columns , function( key, val ) {	
		table += '<th class="'+key+'" js_table_sort sorting>'+val+'</th>';
		 });
	table +='</tr>';
	table += '</thead>';
	table += '<tbody>';	
	var i = 0;
	$.each(data.records, function( key, val ) {
		if(i%2)
			table += '<tr id="tr_'+val['Id']+'" class="odd reported hoverable">';
		else
			table +='<tr id="tr_'+val['Id']+'" class="even hoverable">';
		table += '<td><input type="checkbox" /></td>';
		$.each( data.columns, function( k, v ) {	
			if(k == 'Enable')
			  {
			  	if(v[k] == '1')
			   		table += '<td><input type="checkbox" checked /></td>';
			   	else
			   	   table += '<td><input type="checkbox" /></td>';
			  }
			else
				table += '<td>'+val[k]+'</td>';
		});
		table +='</tr>';
			i++;
			
		 });
	table += '</tbody>';	
	table += '</table>';
	
	
	return table;
}
function paggingAjaxExecute(values,ajaxUrl,paddingDataShowPanel)
{
	
    $(paddingDataShowPanel).empty();
     $.ajax({
     url: ajaxUrl,
     type: "post",
     data: values,
     success: function(data){

         $(paddingDataShowPanel).append(paggingAjaxBuildTable(data));
         $("#pageSize").val(data.pageSize).prop('selected',true);
         var gotoPageOption = "";
         for(var i =1;i <= data.numberRow/data.pageSize+1; i++)
         {
         	if(i==data.gotoPage)
         		gotoPageOption+='<option value="'+i+'" selected>'+i+'</option>';
         	else
         	    gotoPageOption+='<option value="'+i+'">'+i+'</option>';
         }
         $("#gotoPage").empty();
         $("#gotoPage").append(gotoPageOption);
         var pageDisplay = data.fromRecord+" to "+data.toRecord+" of "+data.numberRow+" rows";
         $("#pagedisplay").empty();
         $("#pagedisplay").append(pageDisplay);
         
     },
     error:function(e){
         
         alert(e);
     }
     });
}

$(document).ready(function() {
	$('body').on('change', '#gotoPage, #pageSize', function () {
		
	   var values = 'pageSize='+$('#pageSize').val()+'&gotoPage='+$('#gotoPage').val();
	   var paggingAjaxUrl = $('#paggingAjaxUrl').val();
	   var paddingDataShowPanel =  $('#paddingDataShowPanel').val();
	   paggingAjaxExecute(values,paggingAjaxUrl,paddingDataShowPanel);
	});
	
	$('body').on('click', '.next', function () {
		var pageSize = $('#pageSize').val();
		var theValue = parseInt($("#gotoPage").val()) + 1;
		$("#gotoPage").val( theValue ).prop('selected',true);
	    var paggingAjaxUrl = $('#paggingAjaxUrl').val();
        var paddingDataShowPanel =  $('#paddingDataShowPanel').val();
		var values = 'pageSize='+pageSize+'&gotoPage='+theValue;
		paggingAjaxExecute(values,paggingAjaxUrl,paddingDataShowPanel);
	});
	
	$('body').on('click', '.prev', function () {
		var pageSize = $('#pageSize').val();
		var theValue = parseInt($("#gotoPage").val()) - 1;
		$("#gotoPage").val( theValue ).prop('selected',true);
		var values = 'pageSize='+pageSize+'&gotoPage='+theValue;
	    var paggingAjaxUrl = $('#paggingAjaxUrl').val();
        var paddingDataShowPanel =  $('#paddingDataShowPanel').val();
        paggingAjaxExecute(values,paggingAjaxUrl,paddingDataShowPanel);
		
	});
	$('body').on('click', '.first', function () {
		var pageSize = $('#pageSize').val();
		var theValue = parseInt($("#gotoPage").val()) - 1;
		$("#gotoPage").val( theValue ).prop('selected',true);
		var values = 'pageSize='+pageSize+'&gotoPage='+theValue;
	    var paggingAjaxUrl = $('#paggingAjaxUrl').val();
        var paddingDataShowPanel =  $('#paddingDataShowPanel').val();	
        paggingAjaxExecute(values,paggingAjaxUrl,paddingDataShowPanel);
	});	
	$('body').on('click', '.last', function () {
		$("#gotoPage option:last").attr("selected","selected");
		var pageSize = $('#pageSize').val();
		var gotoPage = $('#gotoPage').val();
		var values = 'pageSize='+pageSize+'&gotoPage='+theValue;
	    var paggingAjaxUrl = $('#paggingAjaxUrl').val();
        var paddingDataShowPanel =  $('#paddingDataShowPanel').val();	
        paggingAjaxExecute(values,paggingAjaxUrl,paddingDataShowPanel);
	});		
});